package utils;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.util.ResourceBundle;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
/**
 * 工具类
 */
public class MybatisUtils {
//    private static SqlSessionFactory sqlSessionFactory = null;
//    // 初始化SqlSessionFactory对象
//    static {
//        try {
//            // 使用MyBatis提供的Resources类加载MyBatis的配置文件
//            Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
//            // 构建SqlSessionFactory工厂
//            sqlSessionFactory =
//                    new SqlSessionFactoryBuilder().build(reader);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
//    // 获取SqlSession对象的静态方法
//    public static SqlSession getSession() {
//        return sqlSessionFactory.openSession();
//    }
public static SqlSession getSqlSession(){
    SqlSession sqlSession=null;
    try {
        InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(is);
        sqlSession= sqlSessionFactory.openSession(true);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return sqlSession;
}
}
